home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / GFXFX2.ZIP / 3D_TMAP2.PAS < prev    next >
Pascal/Delphi Source File  |  1995-02-14  |  4KB  |  118 lines

  1.  
  2. {-$define shaded} { uncomment for flat-shading }
  3.  
  4. program texturemapping; { 3D_TMAP2.PAS }
  5. { Shaded texture-mapping, by Jeroen Bouwens }
  6. uses u_vga,u_ffpcx,u_pal,u_3d,u_kb;
  7. const
  8.   picfile='gfxfx2.pcx';
  9.   picsize=270*180;
  10.   nofpoints=8; { cube has 8 corners }
  11.   nofplanes=12; { 2 triangles/face, 6 faces = 12 triangles }
  12.   points:array[1..nofpoints,0..2] of integer=(
  13.     (10,10,10),(-10,10,10),(-10,-10,10),(10,-10,10),
  14.     (10,10,-10),(-10,10,-10),(-10,-10,-10),(10,-10,-10));
  15.   planes:array[1..nofplanes,0..2] of integer=(
  16.     (1,2,6),(1,6,5),(2,3,7),(2,7,6),(3,4,8),(3,8,7),
  17.     (4,1,5),(4,5,8),(3,2,1),(3,1,4),(5,6,7),(5,7,8));
  18.  
  19. var
  20.   ut,vt:array[1..nofplanes,0..2] of word;
  21.   picture:pointer;
  22.  
  23. procedure initialize; { load texturemap picture and set uv-coords }
  24. var pal:pal_type; i:word; j,k:byte;
  25. begin
  26.   setvideo($13); { set graphics mode }
  27.   fillchar(pal,sizeof(pal),0); setpal(pal); { set all colors to black }
  28.   i:=pcx_load(picfile,vidptr,pal); { drop pcx file on screen }
  29.   getmem(picture,picsize); { reserve memory for texture map }
  30.   for j:=0 to 179 do for i:=0 to 269 do { copy picture to memory }
  31.     mem[seg(picture^):ofs(picture^)+270*j+i]:=mem[$a000:320*j+i];
  32.   cls(vidptr,320*200); { clear screen }
  33.   setpal(pal); { set picture palette }
  34.  
  35.   k:=1; { define the uv-coordinates for every point in every surface }
  36.   for i:=0 to 1 do for j:=0 to 2 do begin
  37.     ut[k,0]:=j*89; vt[k,0]:=i*89;
  38.     ut[k,1]:=j*89+88; vt[k,1]:=i*89;
  39.     ut[k,2]:=j*89+88; vt[k,2]:=i*89+88;
  40.     inc(k);
  41.     ut[k,0]:=j*89; vt[k,0]:=i*89;
  42.     ut[k,1]:=j*89+88; vt[k,1]:=i*89+88;
  43.     ut[k,2]:=j*89; vt[k,2]:=i*89+88;
  44.     inc(k);
  45.   end;
  46. end;
  47.  
  48. procedure rotate_object;
  49. const
  50.   depth=250;
  51.   xst=1; yst=2; zst=2;
  52. var
  53.   xa,ya,za:array[1..nofpoints] of real; { rotated object coords }
  54.   bx,by:array[1..nofpoints] of integer; { 2d coords }
  55.   virscr:pointer;
  56.   relx1,relx2,relz1,relz2,rely1,rely2:real;
  57.   vx,vy,vz,ux,uy,uz,ul:real;
  58.   ll:real;
  59.   xt,yt,zt:real;
  60.   costheta:real;                { angle between lightsource and plane-normal }
  61.   lx,ly,lz:integer;                                     { lightsource coords }
  62.   phix,phiy,phiz,                                 { angles of rotated object }
  63.   surfcol,                                     { surface illumination factor }
  64.   i:byte;
  65. begin
  66.   lx:=0; ly:=0; lz:=15; { light source coordinates }
  67.   ll:=sqrt(lx*lx+ly*ly+lz*lz);
  68.   phix:=0; phiy:=0; phiz:=0;
  69.   getmem(virscr,320*200); { reserve memory for virtual screen }
  70.   destenation:=virscr; destseg:=seg(destenation^); { set new destenation }
  71.   repeat
  72.     for i:=1 to nofpoints do begin
  73.       xt:=points[i,0]; yt:=points[i,1]; zt:=points[i,2]; { get original }
  74.       rrotate(xt,yt,zt,phix,phiy,phiz); { rotate it }
  75.       zt:=zt+60;
  76.       xa[i]:=xt; ya[i]:=yt; za[i]:=zt-20;
  77.       bx[i]:=160+round((xt*depth)/zt); { convert to 2d }
  78.       by[i]:=100+round((yt*depth*0.8333)/zt);
  79.     end;
  80.     cls(virscr,320*200);
  81.     for i:=1 to nofplanes do
  82.       if not checkfront(bx[planes[i,0]],by[planes[i,0]],
  83.                         bx[planes[i,1]],by[planes[i,1]],
  84.                         bx[planes[i,2]],by[planes[i,2]]) then begin
  85.         {$ifdef shaded}
  86.         relx1:=xa[planes[i,1]]-xa[planes[i,0]]; { get illumination factor }
  87.         rely1:=ya[planes[i,1]]-ya[planes[i,0]];
  88.         relz1:=za[planes[i,1]]-za[planes[i,0]];
  89.         relx2:=xa[planes[i,2]]-xa[planes[i,0]];
  90.         rely2:=ya[planes[i,2]]-ya[planes[i,0]];
  91.         relz2:=za[planes[i,2]]-za[planes[i,0]];
  92.         ux:=rely1*relz2-rely2*relz1;
  93.         uy:=relz1*relx2-relz2*relx1;
  94.         uz:=relx1*rely2-relx2*rely1;
  95.         ul:=sqrt(ux*ux+uy*uy+uz*uz);
  96.         costheta:=-(ux*lx+uy*ly+uz*lz)/(ll*ul);
  97.         if costheta<0 then surfcol:=round(31*costheta) else surfcol:=0;
  98.         {$else}
  99.         surfcol:=0;
  100.         {$endif}
  101.         texture(bx[planes[i,0]],by[planes[i,0]],ut[i,0],vt[i,0], { draw plane }
  102.                 bx[planes[i,1]],by[planes[i,1]],ut[i,1],vt[i,1],
  103.                 bx[planes[i,2]],by[planes[i,2]],ut[i,2],vt[i,2],
  104.                 seg(picture^),ofs(picture^),surfcol);
  105.       end;
  106.     flip(virscr,vidptr,320*200); { display picture }
  107.     inc(phix,xst); inc(phiy,yst); inc(phiz,zst); { increase angles }
  108.   until keypressed;
  109.   freemem(virscr,320*200);
  110. end;
  111.  
  112. begin
  113.   initialize;
  114.   rotate_object;
  115.   freemem(picture,picsize);
  116.   setvideo(u_lm);
  117. end.
  118.